package com.ossjk.qlh.minapp.controller;

import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.minapp.OrderID.IOrderService;
import com.ossjk.qlh.minapp.OrderID.OrderType;
import com.ossjk.qlh.minapp.entity.Orders;
import com.ossjk.qlh.minapp.service.IEnterpriseService;
import com.ossjk.qlh.minapp.service.IOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 购物车
 *
 * @author parker
 * @since 2020-08-12
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("/api/mycart")
public class MyCartApiController extends BaseController {

    @Autowired
    private IOrdersService iOrdersService;

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private IEnterpriseService iEnterpriseService;

    /**
     * 购物车支付成功后，响应的接口
     */
    @ApiOperation(value = "购物车支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "套餐id组", paramType = "query", dataType = "Arrays"),
            @ApiImplicitParam(name = "mid", value = "成员id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "订单时段", paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "phone", value = "电话", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "eid", value = "企业id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sendway", value = "配送方式", dataType = "String"),
            @ApiImplicitParam(name = "sendtm", value = "配送时间", dataType = "Date"),
            @ApiImplicitParam(name = "maddrs", value = "详细地址", dataType = "String"),
            @ApiImplicitParam(name = "cost", value = "金额", paramType = "query", dataType = "float"),
            @ApiImplicitParam(name = "sendcost", value = "额定配送费", dataType = "float"),
            @ApiImplicitParam(name = "sendact", value = "实际配送费", dataType = "float"),
    })
    @PostMapping(value = "/orderpayment")
    public ResponseBean orderpayment(@RequestBody RequestBean requestBean) {
        //获取参数
        List ids = (List) requestBean.getParam().get("ids");
        String mid = (String) requestBean.getParam().get("mid");
        String eid = (String) requestBean.getParam().get("eid");
        Integer type = (Integer) requestBean.getParam().get("type");
        String phone = (String) requestBean.getParam().get("phone");

        //配送时间与方式 需要第三方接口
//        String sendway = (String) requestBean.getParam().get("sendway");
//
//        Date sendtm = (Date) requestBean.getParam().get("sendtm");

        String maddrs = (String) requestBean.getParam().get("maddrs");
        BigDecimal cost = new BigDecimal((String) requestBean.getParam().get("cost"));


//        BigDecimal sendcost = (BigDecimal) requestBean.getParam().get("sendcost");
//        BigDecimal sendact = (BigDecimal) requestBean.getParam().get("sendact");


        //订单号生成
        String numcode = iOrderService.generateOrderNum(OrderType.DD_ORDER);

        //生成订单
        Orders orders = new Orders();
        orders.setMid(mid);
        orders.setEid(eid);
        orders.setCode(numcode);
        orders.setType(type);
        orders.setPhone(phone);
        orders.setState(0);
//        orders.setSendway(sendway);
//        //配送时间
//        orders.setSendtm(sendtm);
        orders.setEaddrs(iEnterpriseService.getById(eid).getAddress());
        if (!CommonUtil.isBlank(maddrs)) {
            orders.setMaddrs(maddrs);
        }

        orders.setCost(cost);
//        orders.setSendcost(sendcost);
//        orders.setSendact(sendact);

        //执行 下单封装的方法，新增订单录入信息
        if (iOrdersService.preOrder(orders, ids)) {
            return responseSuccess("下单成功!");
        } else {
            return responseFail("下单失败!");
        }
    }

}
